//
//  MusicViewController.m
//  MoodDiary
//
//  Created by X on 2022/1/24.
//

#import "MusicViewController.h"
#import "MoodDiary-Swift.h"
#import "MusicCollectionViewCell.h"
#import "GetModel.h"
#import "GetListModel.h"

@interface MusicViewController ()<
UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
{
    NSMutableArray * _arrdata;
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    _arrdata=[NSMutableArray new];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowLayout.minimumInteritemSpacing = KW(20);
    self.flowLayout.minimumLineSpacing = KW(20);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(KW(20), DEF_StatusBarAndNavigationBarHeight+KW(20), SCREEN_WIDTH-KW(40), SCREEN_HEIGHT-DEF_StatusBarAndNavigationBarHeight-KW(20)) collectionViewLayout:self.flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
////        self.collectionView.mj_header =[MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
//    self.collectionView.mj_footer =[MJChiBaoZiFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    //注册cell
    [self.collectionView registerClass:[MusicCollectionViewCell class] forCellWithReuseIdentifier:kCellIdentifier_CollectionView];
    [self.view addSubview:self.collectionView];
    [self data];
    // Do any additional setup after loading the view.
}

-(void)data{
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [BANetManager ba_requestWithType:BAHttpRequestTypeGet
                           urlString:@"https://pdotools-api.mmtravel.cn/api/Sleepaid/getzmlist"
                          parameters:dic
     
                        successBlock:^(id response) {
                                if ([[response objectForKey:@"status"] intValue] == 1) {
                                    [self->_arrdata removeAllObjects];
                                    GetModel *item = [GetModel objectFromDictionary:[response objectForKey:@"data"]];
                                    for (int i =0; i < item.list.count; i++) {
                                        GetListModel *modelCa=[[GetListModel alloc]init];
                                        modelCa=item.list[i];
                                        modelCa.isPlay =@"0";
                                    }
                                    [self->_arrdata addObjectsFromArray:item.list];
                                    [self.collectionView reloadData];
                                } else {
                                    
                                }
                                NSLog(@"post请求数据成功： *** %@", response);
                            
                        } failureBlock:^(NSError *error) {
                            NSLog(@"post请求数据失败： *** %@", error);
                        } progress:^(int64_t bytesProgress, int64_t totalBytesProgress) {
                            
                        }];
}

#pragma mark - UICollectionView DataSource Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrdata.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier_CollectionView forIndexPath:indexPath];
    GetListModel *modelCa=[[GetListModel alloc]init];
    modelCa=_arrdata[indexPath.row];
    [cell.imageV setImageURL:[NSString stringWithFormat:@"%@",modelCa.card_cover_url]];
    cell.name.text = modelCa.sub_title;
    if ([modelCa.isPlay integerValue] == 1) {
        cell.backView.layer.borderColor = COLOR(255, 216, 119, 1).CGColor;
        cell.imagePlay.image = [UIImage imageNamed:@"音乐-播放中"];
    }else{
        cell.backView.layer.borderColor = COLOR(232, 232, 232, 1).CGColor;
        cell.imagePlay.image = [UIImage imageNamed:@"开始播放"];
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(KW(156),
                      KW(180));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GetListModel *modelAa=[[GetListModel alloc]init];
    modelAa=_arrdata[indexPath.row];
    if ([modelAa.isPlay integerValue] == 1) {
        modelAa.isPlay=@"0";
        [[NSNotificationCenter defaultCenter] postNotificationName:@"stopMusic" object:nil];
    }else{
        for (int i =0; i < _arrdata.count; i++) {
            GetListModel *mod=[[GetListModel alloc]init];
            mod=self->_arrdata[i];
            mod.isPlay =@"0";
        }
        
        GetListModel *modelCa=[[GetListModel alloc]init];
        modelCa=_arrdata[indexPath.row];
        modelCa.isPlay=@"1";
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getMusic" object:modelCa.track_id];
    }

    
    [self.collectionView reloadData];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
